package com.styles.codingstyle.services.impl;

import com.styles.codingstyle.dataobject.CacheTestEntity;
import com.styles.codingstyle.mapper.CacheTestMapper;
import com.styles.codingstyle.services.CacheTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Package com.styles.codingstyle.services.impl
 * @Description: 缓存服务实现类
 * @Author 陈建
 * @Create: 2018-06-12 09:22
 * @Version 1.0
 * @Update: message
 */
@Service
@Transactional
public class CacheTestServiceImpl implements CacheTestService {

    @Autowired
    private CacheTestMapper cacheTestMapper;


    @Override
    @Cacheable(cacheNames = "cache20")
    public List<CacheTestEntity> findAll() {
        List<CacheTestEntity> list = cacheTestMapper.findAll();
        System.out.println("测试缓存");
        for (CacheTestEntity cc : list) {
            System.out.println(cc);
        }
        return list;
    }


    @Override
    @CacheEvict(cacheNames = "cache20")
    public void add() {
        CacheTestEntity cacheTestEntity = new CacheTestEntity();
        cacheTestEntity.setStr1(System.currentTimeMillis() + "");
        cacheTestEntity.setStr2(System.currentTimeMillis() + "");
        cacheTestEntity.setStr3(System.currentTimeMillis() + "");
        cacheTestEntity.setStr4(System.currentTimeMillis() + "");
        cacheTestEntity.setStr5(System.currentTimeMillis() + "");
        cacheTestMapper.saveAndFlush(cacheTestEntity);
    }
}
